package com.example.android.bluetoothchat.uitls;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Administrator on 2017/4/23 0023.
 */

public class ShareUtils {

    private static final String PREF_CONFIG = "pref_config";
    static Context mContext;
    static SharedPreferences preferences;

    public static void init(Context context) {
        mContext = context;
        getSharef();
    }
    
    /**
     * 获取SharedPreferences单类
     *
     * @return
     */
    private static SharedPreferences getSharef(){
        if(preferences == null && mContext != null){
            preferences = mContext.getSharedPreferences(PREF_CONFIG, Context.MODE_PRIVATE);
        }
        return preferences;
    }
    
    
    /**
     * 保存字符串
     *
     * @param key 键
     * @param value 值
     */
    public static void putString(String key, String value) {
        getSharef().edit().putString(key ,value).commit();
    }
    
    /**
     * 获取保存的字符串
     *
     * @param key 键
     * @param defvalue 默认值
     * @return
     */
    public static String getString(String key, String defvalue) {
        return getSharef().getString(key ,defvalue);
    }
    
    /**
     * 保存boolean值
     *
     * @param key 键
     * @param value 值
     */
    public static void putBoolean(String key, boolean value) {
        getSharef().edit().putBoolean(key ,value).commit();
    }
    
    /**
     * 获取保存的boolean值
     *
     * @param key 键
     * @param defvalue 默认值
     * @return
     */
    public static boolean getBoolean(String key, boolean defvalue) {
        return getSharef().getBoolean(key ,defvalue);
    }


}
